﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using Styx.CommonBot;

namespace UltimatePvPSuite
{
    public static class Refreshments
    {
        //Locate Refresment Table

        private static string RefreshmentTableName = @"World\Goober\G_ConjureTable.mdx";
        private static WoWGameObject RefreshmentTable
        {
            get
            {
                using (StyxWoW.Memory.AcquireFrame())
                {
                    return (from Unit in ObjectManager.GetObjectsOfType<WoWGameObject>(false)
                            orderby Unit.Distance
                            where Unit.Model.Equals(RefreshmentTableName)
                            where Unit.Distance < 45
                            select Unit).FirstOrDefault();
                }
            }
        }
        //Locate Soulwell
        private static string SoulwellName = @"World\Goober\G_AltarOfSouls.mdx";
        private static WoWGameObject Soulwell
        {
            get
            {
                using (StyxWoW.Memory.AcquireFrame())
                {
                    return (from Unit in ObjectManager.GetObjectsOfType<WoWGameObject>(false)
                            orderby Unit.Distance
                            where Unit.Model.Equals(SoulwellName)
                            where Unit.Distance < 45
                            select Unit).FirstOrDefault();
                }
            }
        }
        //Pulse
        public static void Pulse()
        {
            Create();
            PickUp();
        }
        private static void PickUp()
        {
            int manaCake;
            int manaAttempt = 0;
            int shardAttempt = 0;
            if (StyxWoW.Me.Level >= 65 && StyxWoW.Me.Level < 80)
            {
                manaCake = 34062;
            }
            else if (StyxWoW.Me.Level >= 80 && StyxWoW.Me.Level < 85)
            {
                manaCake = 43523;
            }
            else
            {
                manaCake = 65499;
            }

            WoWPoint moveTo = StyxWoW.Me.Location;            
            if (StyxWoW.Me.HasAura("Preparation"))
            {
                if (RefreshmentTable != null && manaAttempt < 4 && StyxWoW.Me.BagItems.Count(i => i != null && i.IsValid && i.Entry == manaCake) < 4 && Battlegrounds.Current != BattlegroundType.SotA && UltimatePvPSuite.EnableUseRefreshments)
                {
                    moveTo = WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, RefreshmentTable.Location, 3f);

                    if (Navigator.CanNavigateFully(StyxWoW.Me.Location, moveTo))
                    {
                        while (StyxWoW.Me.BagItems.Count(i => i != null && i.IsValid && i.Entry == 65499) < 4 && StyxWoW.Me.HasAura("Preparation") && manaAttempt < 4)
                        {
                            BotPoi.Current = new BotPoi(RefreshmentTable, PoiType.Hotspot);
                            Navigator.MoveTo(moveTo);
                            if (RefreshmentTable.Distance <= 5f)
                            {
                                manaAttempt++;
                                Logger.Write("Trying to get food from: " + RefreshmentTable.Name.ToString());
                                RefreshmentTable.Interact();
                            }
                        }
                    }
                }
                else if (Soulwell != null && shardAttempt < 1 && StyxWoW.Me.BagItems.Count(i => i != null && i.IsValid && i.Entry == 5512) < 1 && Battlegrounds.Current != BattlegroundType.SotA && UltimatePvPSuite.EnableUseSoulwell)
                {
                    moveTo = WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, Soulwell.Location, 3f);

                    if (Navigator.CanNavigateFully(StyxWoW.Me.Location, moveTo))
                    {
                        while (StyxWoW.Me.BagItems.Count(i => i != null && i.IsValid && i.Entry == 5512) < 1 && StyxWoW.Me.HasAura("Preparation") && shardAttempt < 1)
                        {
                            BotPoi.Current = new BotPoi(Soulwell, PoiType.Hotspot);
                            Navigator.MoveTo(moveTo);
                            if (Soulwell.Distance <= 5f)
                            {
                                shardAttempt++;
                                Logger.Write("Trying to get HealthStone from: " + Soulwell.Name.ToString());
                                Soulwell.Interact();
                            }
                        }
                    }
                }
            }
        }
        //Creates Refreshment Table or Soul Well
        private static void Create()
        {
            if (StyxWoW.Me.HasAura("Preparation") && Styx.WoWInternals.Battlegrounds.BattlefieldInstanceRunTime.Seconds < 60)
            {
                if (RefreshmentTable == null && StyxWoW.Me.Class == Styx.WoWClass.Mage
                    && UltimatePvPSuite.EnableCreateRefreshments && !StyxWoW.Me.IsMoving)
                {
                    if (SpellManager.CanCast(43987))
                    {
                        Navigator.PlayerMover.MoveStop();
                        if (StyxWoW.Me.Mounted)
                            Lua.DoString("Dismount()");
                        SpellManager.Cast(43987);
                        while (StyxWoW.Me.IsCasting && StyxWoW.Me.HasAura("Preparation"))
                        {
                            Thread.Sleep(500);
                        }
                    }
                }

                if (Soulwell == null && StyxWoW.Me.Class == Styx.WoWClass.Warlock
                    && UltimatePvPSuite.EnableCreateSoulwell && !StyxWoW.Me.IsMoving)
                {
                    if (SpellManager.CanCast(29893))
                    {
                        Navigator.PlayerMover.MoveStop();
                        if (StyxWoW.Me.Mounted)
                            Lua.DoString("Dismount()");
                        SpellManager.Cast(29893);
                        while (StyxWoW.Me.IsCasting && StyxWoW.Me.HasAura("Preparation"))
                        {
                            Thread.Sleep(500);
                        }
                    }
                }
            }
        }
    }
}